package org.ielse.frame.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.ielse.frame.App;
import org.ielse.frame.R;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.text.format.Time;

@SuppressLint("SimpleDateFormat")
public class DateUtil {

	/** yyyy年MM月dd日 HH:mm:ss */
	public static final String yyyy_MM_dd_HH_mm_ss = "yyyy\u5e74MM\u6708dd\u65e5 HH:mm:ss";
	/** MM月dd日 */
	public static final String MM_dd = "MM-dd";
	public static final String MM_dd_ = "MM\u6708dd\u65e5";
	public static final String yyyyMMddHHmm = "yyyyMMddHHmm";
	public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static final String HHmmss = "HHmmss";
	public static final String dd = "dd";
	public static final String a_HH_mm = "a HH:mm";

	public static final int dailySeconds = 86400;

	private static final SimpleDateFormat sdf = new SimpleDateFormat();
	private static String[] timeDayInfo;
	private static String timeYesterday;
	private static String timeTwoDaysAgo;
	private static String timeMonthAgo;
	private static String timeTwoMonthsAgo;
	private static String timeLongTimeAgo;

	static {
		Resources res = App.i().getResources();
		timeDayInfo = res.getStringArray(R.array.frame_time_dayinfo);
		timeYesterday = res.getString(R.string.frame_time_yesterday);
		timeTwoDaysAgo = res.getString(R.string.frame_time_two_days_ago);
		timeMonthAgo = res.getString(R.string.frame_time_month_ago);
		timeTwoMonthsAgo = res.getString(R.string.frame_time_two_months_ago);
		timeLongTimeAgo = res.getString(R.string.frame_time_long_time_ago);
	}

	public static Date parse(String date, String pattern) throws ParseException {
		synchronized (sdf) {
			sdf.applyPattern(pattern);
			return sdf.parse(date);
		}
	}

	public static String format(Date date, String pattern) throws ParseException {
		synchronized (sdf) {
			sdf.applyPattern(pattern);
			return sdf.format(date);
		}
	}

	public static String convert(String string, String oldPattern, String newPattern) throws ParseException {
		return format(parse(string, oldPattern), newPattern);
	}

	public static long intervalSeconds(Date date1, Date date2) {
		return (date1.getTime() - date2.getTime()) / 1000;
	}

	public static String timeInfo(Date date) throws ParseException {
		long nowTimestamp = System.currentTimeMillis();
		long seconds = (nowTimestamp - date.getTime()) / 1000;
		if (seconds < 0) {
			throw new IllegalArgumentException("just support compare to the past");
		}
		Time time = new Time();
		time.set(nowTimestamp);
		int nowYear = time.year;
		int nowMonth = time.month;
		int nowMonthDay = time.monthDay;

		if (seconds < dailySeconds * 7) {
			time.set(date.getTime());
			if (nowMonthDay == time.monthDay && nowMonth == time.month && nowYear == time.year) {
				return format(date, a_HH_mm);
			}
			time.set(date.getTime() + dailySeconds * 1000);
			if (nowMonthDay == time.monthDay && nowMonth == time.month && nowYear == time.year) {
				return timeYesterday;
			}
			time.set(date.getTime() + dailySeconds * 2000);
			if (nowMonthDay == time.monthDay && nowMonth == time.month && nowYear == time.year) {
				return timeTwoDaysAgo;
			}

			time.set(date.getTime());
			return timeDayInfo[time.weekDay];
		}
		else if (seconds < dailySeconds * 30) {
			return format(date, MM_dd);
		}
		else if (seconds < dailySeconds * 60) {
			return timeMonthAgo;
		}
		else if (seconds < dailySeconds * 90) {
			return timeTwoMonthsAgo;
		}

		return timeLongTimeAgo;
	}
}
